package test.client43;

import java.util.logging.Level;
import java.util.logging.Logger;

import net.m510.autosurfer.core.log.LogUtils;

import org.apache.http.HttpEntity;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class ClientTest02 {
	private static final Logger log = Logger.getLogger(ClientTest02.class.getName());
	public static void main( String args[] ) throws Exception {
		new ClientTest02().go();
	}

	private void go() throws Exception {
		RequestConfig rc = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH).build();
		CookieStore cs = new BasicCookieStore();
		CloseableHttpClient cl = HttpClients.custom()
				.setDefaultRequestConfig(rc)
				.setDefaultCookieStore(cs)
				.build();
		HttpGet get = new HttpGet("http://www.yahoo.co.jp/");
		log.info(get.toString());
		CloseableHttpResponse r = cl.execute(get);
		try {
			log.info(r.getStatusLine().toString());
			HttpEntity e = r.getEntity();
			LogUtils.logCookies(log, Level.INFO, cs.getCookies(), "this is cookies");
//			log.info("this is cookies");
//			for ( Cookie c : cs.getCookies()) {
//				log.info(c.toString());
//			} 
			EntityUtils.consume(e);
		} finally {
			r.close();
		}
	}
}
